#!/bin/csh -f
#
# assignchk: Shell script to check the consistency of stereospecific
# assignments, using AquaAssign and other Aqua modules.
# =============================================================
# Jurgen Doreleijers and Ton Rullmann, Dec 12, 1997 - Aqua 1.5
# =============================================================
#
# The variable 'aquaroot' must exist and point to the Aqua top directory.
# The 'aqsetup' script should reside in that directory.
#

if ( $#argv < 1 ) goto usage
goto begin

#####################
# usage block
#####################
usage:
    echo 'usage: assignchk  [options]  coordinates_file  [ranges_files]'
    echo ''
    echo '   options (can be in any order):'
    echo '   [-c]  [-r name]  [-ext]  [-terseness value]'
    echo '   [-xplor|molmol]  [-readass ID]'
    echo ''
    echo '   -c:       prevent cleanup'
    echo '   -r:       file name (minus extension) of Aqua restraint files'
    echo '             (if not given, name is derived from coordinates file name)'
    echo '   -ext:     use the external (original) names to match restraint atoms'
    echo '             to coordinate atoms (default is to use internal names)'
    echo '   -terseness value: [0-9] integer specifying decreasing verbosity'
    echo '   -xplor:   coordinates file is an XPLOR-style PDB file'
    echo '   -molmol:  coordinates file is a MOLMOL-style PDB file'
    echo''
    echo '   -readass ID:  read assignments from file PROJECT_ass_ID.ass'
    exit (1)

#####################
# initialization
#####################
begin:
    source $aquaroot/aqsetup

    set errstatus = 0
    set cleanup   = 1
    set coordinp  = ""
    set restrname = ""
    set oldmap    = ""
    set readass   = ""
# ident_all and ident_rng should have identical values as in aqpc
    set ident_all  = "pc"
    set ident_ste  = "pca"
    set ident_rng  = "rng"
    set ident_rngs = "rnga"
    set script_log = "assignchk.log"
    setenv AQUATERSENESS 0     # most verbose messages

#####################
# process the options
#####################
options:
switch ( "$1" )
    case "-c":
        set cleanup = 0
	shift
        breaksw
    case "-r":
	if ( $#argv < 1 ) goto usage
	shift
	set restrname = $1
	shift
	breaksw
    case "-ext":
	set oldmap = "-ext"
	shift
	breaksw
    case "-xplor":
    case "-molmol":
	set coordinp = "$1"
	shift
	breaksw
    case "-readass":
	shift
	set readass = "$1"
	shift
	breaksw
    case "-terseness":
	shift        
	setenv AQUATERSENESS $1 # Inherited by all processes because using setenv
	shift
	breaksw
    default:
        goto endoptions
endsw
goto options

endoptions:
    if ( $#argv < 1 ) goto usage

# Check options for acceptable ranges
if ( ( $AQUATERSENESS < 0 ) || ( $AQUATERSENESS > 9 ) ) then
    echo "The parameter terseness (specified to be $AQUATERSENESS) can only"
    echo "take integer values from 0 through 9"
    exit(1)
endif

# Set level of terseness as a category which can be modified later on
if ( $AQUATERSENESS < 3 ) then
    set terselevel = "verbose"
else if ( $AQUATERSENESS < 6 ) then
    set terselevel = "curbed"
else
    set terselevel = "terse"
endif

# get started

# Initialize the log file
    \rm $script_log >& /dev/null

    if ( $terselevel != "verbose" ) then
        echo 'USE A LOWER VALUE FOR TERSENESS TO GET MORE OUTPUT IN THIS FILE IF NEEDED'    >> $script_log
        echo ' '                                                                            >> $script_log
    endif

# set coordinates file
    set coords  = $1
# setup project name
    set proj = `qgetproj $coords`
    if ( $terselevel == "verbose" ) then
        echo 'Aqua program directory:' $aquaroot
        echo 'ASSIGNCHK - Checking stereospecific assignments'
        echo 'ASSIGNCHK - Project name is' $proj
    endif    
    qproject $proj
# set name (minus extension) of Aqua restraint file
    if ( $restrname == "" ) set restrname = $proj
# set output idents
    if ( $#argv == 1 ) then
	set ident  = $ident_all
	set ident2 = $ident_ste
    else
	set ident  = $ident_rng
	set ident2 = $ident_rngs
    endif

# run aqpc
    echo 'ASSIGNCHK - Running aqpc'
    touch aqua.cmm
    \cp aqua.cmm aqua.cmm.bak
    echo "set sho-selonly off" >> aqua.cmm
    aqpc -c -nosum -terseness $AQUATERSENESS -r $restrname $oldmap $coordinp $argv[*] >> $script_log
    set errstatus = $status
    if ( $errstatus ) then
	echo 'ASSIGNCHK - Error in aqpc, see aqpc.log'
	\mv aqua.cmm.bak aqua.cmm
	exit (1)
    endif

# prepare for assignment check, and run qassign
    echo 'ASSIGNCHK - Running qhow'
    qhow set batch on set terseness $AQUATERSENESS exec aqua \
        readinf noe $ident stereo assign $ident2 quit >> $script_log

    if ( $status ) then
	echo 'assignchk - Qhow failed the first time in making the right info'
	echo 'assignchk - Running qpseudo first and then qhow again'

        set type= (`gawk '($1=="count")&&($3=="type") {print $4}' $restrname.noe`)

	qpseudo set batch on set terseness $AQUATERSENESS \
            mol orig  pc make $type write explodedSet pc quit >> $script_log
	qhow    set batch on set terseness $AQUATERSENESS \
            mol explo pc noe aqua $restrname.noe $type make pc stereo assign $ident2 quit >> $script_log
    endif

    echo 'ASSIGNCHK - Running qdist'
    qdist set batch on set terseness $AQUATERSENESS \
        do extended noe $ident2 >> $script_log
    echo 'ASSIGNCHK - Running qassign'
    touch assign.cmm
    \cp assign.cmm assign.cmm.bak
    if ( "$readass" != "" ) echo "set readass t" >> assign.cmm
    qassign set batch on set terseness $AQUATERSENESS \
        exec assign do noe $ident2 $readass aqua aqua assigned.noe 'assigned'
    \mv assign.cmm.bak assign.cmm

# cleanup files, if not turned off
    \mv aqua.cmm.bak aqua.cmm
    if ( $cleanup ) then
	echo ' '
        echo 'ASSIGNCHK - Cleaning up for project' $proj
	aqdrst clean $ident
	aqdrst clean $ident2
	if ( $#argv == 2 ) then
	    aqdrst clean $ident_all
	endif
	\rm -f $proj'_pseudo.pdb'
    endif
